package eu.codlab.mouse_client;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.Socket;

import android.os.AsyncTask;
import android.os.Handler;

/**
 * Class to manage the infos to send to the server on the PC
 * @author kevin
 *
 */
public class Client extends AsyncTask<Integer, Double, Long>{
	static Socket clientSocket;
	static DataOutputStream outToServer;
	static BufferedReader inFromServer;
	static Handler _handler;
	static String _server;
	static int _port;

	private void first(){
		if(_handler == null)
			_handler = new Handler();
		try{
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	public Client(){
		first();
	}

	public Client(String server, int port){
		_server = server;
		_port = port;
		first();
	}
	public boolean init(){
		//the computer IP and the port it is listening to
		boolean final_state = true;
		try{
		destroy();
		clientSocket = new Socket(_server, _port);
		outToServer = new DataOutputStream(clientSocket.getOutputStream());
		inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
		}catch(Exception e){
			final_state = false;
			destroy();
		}
		
		return final_state;
	}
	public void destroy(){
		try{
			if(clientSocket != null){
				if(clientSocket.isConnected())
					clientSocket.close();
				if(!clientSocket.isInputShutdown())
					clientSocket.shutdownInput();
				if(!clientSocket.isOutputShutdown())
					clientSocket.shutdownOutput();
			}
			clientSocket = null;
			outToServer = null;
			inFromServer = null;
		}catch(Exception e){
		}
	}
	public boolean isInit(){
		return clientSocket != null && clientSocket.isConnected();
	}
	@Override
	protected Long doInBackground(final Integer... params) {
		if(!isInit())
			try {
				init();
			} catch (Exception e1) {
				e1.printStackTrace();
				clientSocket = null;
				outToServer = null;
				inFromServer = null;
			}
		_handler.post(new Thread(){
			public void run(){
				try{
					byte _entier[]=new byte[4];
					//quite a dirty way to make BigEndian from LittleEndian
					//there are cleaner way but it is ok x)
					_entier[3] = (byte) (params[0] & 0xff);
					_entier[2] = (byte) ((params[0]>>8) & 0xff);
					_entier[1] = (byte) ((params[0]>>16) & 0xff);
					_entier[0] = (byte) ((params[0]>>24) & 0xff);
					outToServer.write(_entier);
					_entier[3] = (byte) (params[1] & 0xff);
					_entier[2] = (byte) ((params[1]>>8) & 0xff);
					_entier[1] = (byte) ((params[1]>>16) & 0xff);
					_entier[0] = (byte) ((params[1]>>24) & 0xff);
					outToServer.write(_entier);
					_entier[3] = (byte) (params[2] & 0xff);
					_entier[2] = (byte) ((params[2]>>8) & 0xff);
					_entier[1] = (byte) ((params[2]>>16) & 0xff);
					_entier[0] = (byte) ((params[2]>>24) & 0xff);
					outToServer.write(_entier);
					outToServer.flush();
				} catch (Exception e) {
					e.printStackTrace();
					clientSocket = null;
					outToServer = null;
					inFromServer = null;
					Client.this.destroy();
				}
			}
		});
		return null;
	}
}